// pages/seal/paymentTest/paymentTest.js
import config from '../../../utils/config';
import PaymentUtil from '../../../utils/payment';

Page({
  data: {
    testAmount: '0.01',
    testPhone: '13800138000',
    testRemark: '测试订单',
    orderId: '',
    paymentStatus: '',
    loading: false
  },

  // 输入金额
  onAmountInput(e) {
    this.setData({
      testAmount: e.detail.value
    });
  },

  // 输入手机号
  onPhoneInput(e) {
    this.setData({
      testPhone: e.detail.value
    });
  },

  // 输入备注
  onRemarkInput(e) {
    this.setData({
      testRemark: e.detail.value
    });
  },

  // 测试支付
  async testPayment() {
    try {
      this.setData({ loading: true });

      const token = wx.getStorageSync('token');
      if (!token) {
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        });
        return;
      }

      // 准备测试订单数据
      const orderData = {
        phone: this.data.testPhone,
        totalPrice: this.data.testAmount,
        addressIndex: 0,
        remark: this.data.testRemark
      };

      console.log('测试订单数据:', orderData);

      // 创建订单
      const orderInfo = await PaymentUtil.createOrder(orderData, token);
      console.log('订单创建成功:', orderInfo);

      this.setData({
        orderId: orderInfo.orderId
      });

      // 验证支付参数
      if (!PaymentUtil.validatePaymentParams(orderInfo.paymentParams)) {
        throw new Error('支付参数无效');
      }

      console.log('支付参数:', orderInfo.paymentParams);

      // 调用微信支付
      await PaymentUtil.requestPayment(orderInfo.paymentParams);
      
      // 支付成功
      this.setData({
        paymentStatus: 'success'
      });

      wx.showToast({
        title: '支付成功',
        icon: 'success'
      });

    } catch (error) {
      console.error('支付测试失败:', error);
      
      this.setData({
        paymentStatus: 'failed'
      });

      wx.showModal({
        title: '支付测试失败',
        content: error.message || '未知错误',
        showCancel: false
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 查询订单状态
  async queryOrderStatus() {
    if (!this.data.orderId) {
      wx.showToast({
        title: '请先创建订单',
        icon: 'none'
      });
      return;
    }

    try {
      this.setData({ loading: true });

      const token = wx.getStorageSync('token');
      const orderStatus = await PaymentUtil.queryOrderStatus(this.data.orderId, token);
      
      console.log('订单状态:', orderStatus);

      wx.showModal({
        title: '订单状态',
        content: `订单号: ${orderStatus.orderId}\n状态: ${orderStatus.status}\n金额: ¥${orderStatus.amount}`,
        showCancel: false
      });

    } catch (error) {
      console.error('查询订单状态失败:', error);
      
      wx.showToast({
        title: '查询失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 清除测试数据
  clearTestData() {
    this.setData({
      orderId: '',
      paymentStatus: '',
      testAmount: '0.01',
      testPhone: '13800138000',
      testRemark: '测试订单'
    });
  },

  // 查看日志
  viewLogs() {
    console.log('当前测试数据:', this.data);
    wx.showModal({
      title: '测试数据',
      content: JSON.stringify(this.data, null, 2),
      showCancel: false
    });
  }
}); 